﻿using nanhua.BookStore.Entitys.OrderAggregate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Uow;

namespace nanhua.BookStore.Orders
{
    /// <summary>
    /// 订单领域种子数据初始化
    /// </summary>
    public class OrderDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        IOrderRepository _orderRepository;
        IGuidGenerator _guidGenerator;
        public OrderDataSeedContributor(IOrderRepository orderRepository, IGuidGenerator guidGenerator)
        {
            this._orderRepository = orderRepository;
            this._guidGenerator = guidGenerator;
        }
        [UnitOfWork]
        public async Task SeedAsync(DataSeedContext context)
        {
            if (await _orderRepository.GetCountAsync() <= 0)
            {
                var order = new Order(_guidGenerator.Create());
                order.AddOrderDetail(new OrderDetail(_guidGenerator.Create(), "P_0001", 34));
                order.AddOrderDetail(new OrderDetail(_guidGenerator.Create(), "P_0002", 35));
                order.Pay(PayTypeEnum.Alipay);
                await _orderRepository.InsertAsync(order);
            }
        }
    }
}
